package org.shagin.jfin.sheet;

import java.awt.Color;
import java.awt.Component;
import java.text.NumberFormat;

import javax.swing.BorderFactory;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;

import org.shagin.jfin.data.util.CurrencyUtil;

// http://www.java2s.com/Code/Java/Swing-Components/ColumnBorderTableExample.htm
// http://www.daniweb.com/software-development/java/threads/130876
// http://www.java2s.com/Code/Java/2D-Graphics-GUI/AnotherGradientPaintDemo.htm
// http://www.esus.com/docs/GetQuestionPage.jsp?uid=1290
/**
 * The Class SheetCellRenderer.
 */
public class SheetCellRenderer extends DefaultTableCellRenderer {

    private static final long serialVersionUID = 988531012615791426L;
    private SheetTableModel model;
    private Color[] bgRows;
    private Color[] bgCols;
    private Color defaultBgColor = Color.WHITE;

    /**
     * Instantiates a new sheet cell renderer.
     * 
     * @param model the model
     */
    public SheetCellRenderer(SheetTableModel model) {
        super();
        this.model = model;
        initBackground();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void setValue(Object aValue) {
        Object result = aValue;
        if ((aValue != null) && (aValue instanceof Double)) {
            Number numberValue = (Double) aValue;

            NumberFormat formatter = NumberFormat.getCurrencyInstance();
            // TODO Define Currency
            result = CurrencyUtil.format(formatter.getCurrency(),
                    numberValue.doubleValue());
        }
        super.setValue(result);
    }

    /**
     * Inits the background.
     */
    public void initBackground() {

        bgRows = new Color[model.getSheet().getRows().size()];
        for (int i = 0; i < model.getSheet().getRows().size(); i++) {
            SheetRow row = model.getSheet().getRows().get(i);
            if (row instanceof SheetRowSubTotal)
                bgRows[i] = Color.LIGHT_GRAY;
            else if (row instanceof SheetRowTotalPercent)
                bgRows[i] = Color.GRAY;
            else
                bgRows[i] = defaultBgColor;
        }

        bgCols = new Color[model.getSheet().getData().getColumnCount()];
        for (int i = 0; i < model.getSheet().getData().getColumnCount(); i++) {
            SheetColumn<?> col = model.getSheet().getData().getColumn(i);
            if (col instanceof SheetColumnCategory)
                bgCols[i] = Color.LIGHT_GRAY;
            else
                bgCols[i] = defaultBgColor;
        }
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Component getTableCellRendererComponent(JTable aTable, Object value,
            boolean aIsSelected, boolean aHasFocus, int row, int col) {
        if (col == 0) {
            if (aIsSelected)
                value = " > ";
            else
                value = "";
        }
        setBackground(getBgColor(row, col));
        if (value instanceof Double) {
            Double d = (Double) value;
            if (d < 0)
                setForeground(Color.RED);
            else
                setForeground(Color.BLACK);
        }
        super.getTableCellRendererComponent(aTable, value, aIsSelected,
                aHasFocus, row, col);
        setBorder(BorderFactory.createEmptyBorder());
        return this;
    }

    /**
     * Gets the bg color.
     * 
     * @param row the row
     * @param col the col
     * @return the bg color
     */
    public Color getBgColor(int row, int col) {
        Color result = defaultBgColor;

        if (!defaultBgColor.equals(bgCols[col]))
            result = bgCols[col];

        if (!defaultBgColor.equals(bgRows[row]))
            result = bgRows[row];

        return result;
    }
}
